-- boss_summoner
-- create by chengb
-- 技能: boss召唤技（野性呼唤）

return {
    apply = function(source, target, skillId, round, para, extra)
        local arr = {};

        -- TODO 限定位置为boss两侧的格子
        local waitList = { 11, 15, 16, 20, 21, 25, 27, 29 };
        local invisibleBlockClass = DungeonM.getInvisibleBlockClass();
        local dbase = SkillM.query(skillId, "dbase");

        if dbase["pos_list"] then
            waitList = dbase["pos_list"];
        end

        -- 先找出所有已使用的空位
        for _, pos in pairs(waitList) do
            local grid = DungeonM.getGridByPos(pos);
            repeat
                -- 未被翻开
                if not grid:isOpened() then
                    break;
                end

                -- 有奖励
                if grid.bonus then
                    -- 此种情况必定不能选
                    if grid.state ~= GRID_STATE_DISABLE then
                        break;
                    end

                    -- 如果原先是怪物则也不能选
                    if grid.monster then
                        break;
                    end
                end

                -- 有钥匙
                if grid.has_key then
                    break;
                end

                -- 空格、失效的格子或者隐形的障碍
                if grid.type == GRID_TYPE_EMPTY or
                   grid.state == GRID_STATE_DISABLE or
                   grid.class == invisibleBlockClass or
                   grid.type == GRID_TYPE_UNREAL_SPECIAL then
                    table.insert(arr, grid:getPos());
                end
            until true;
        end

        -- 如果没有空位
        if #arr <= 0 then
            return {};
        end

        local rand = DungeonM.getRandSeed("summoner_skill");

        local pos = arr[rand % #arr + 1];

        -- 随机选一个召唤物
        local elements = para[1];
        local element = elements[rand % #elements + 1];

        local ishide = flase;
        local monsterDisplay = MonsterDisplayM.getMonsterSkillDisplay(source.dbase:query("class_id"), skillId);
        -- 如果配置了BOSS特效播放，那么就先隐藏一下，等UIBossEffect界面效果播放完了再显示
        if monsterDisplay and monsterDisplay.boss_effect == 1 then
            ishide = true;
        end

        DungeonM.transGridToMonster(pos, element, nil, ishide);

        if not ishide then
            -- 不能出现UI的东西。暂时屏蔽掉，另new任务改
            -- 抛出个场景行为事件，往场景行为序列加入"投掷"行为，用以播放烟尘特效
            -- EventMgr.fire(event.ADD_SCENE_SEQUENCE, {
            --     ["action"]  = "launch",
            --     ["fromPos"] = cc.pAdd(UIDungeonMgr:getCurLevel():getMonsterPos(pos), cc.p(10, 50)),
            --     ["toPos"]   = cc.pAdd(UIDungeonMgr:getCurLevel():getMonsterPos(pos), cc.p(10, 50));
            --     ["args"]    = { ["effect_id"] = 1270, },
            --     });
        end

        return { source, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        local elements = string.explode(arr[1], "+");
        for index, element in pairs(elements) do
            elements[index] = tonumber(element);
        end

        return {elements};
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        return desc;
    end,
}

